/*
 *  Copyright (C) 2010 Glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFormattedTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;

/**
 *
 * @author glacuo
 */
public class ComponentSelectAllText implements FocusListener{

    private static final ComponentSelectAllText instance =
            new ComponentSelectAllText();

    public static ComponentSelectAllText getDefault(){
        return instance;
    }

    @Override
    public void focusGained(FocusEvent e){
        try{
            if (e.getSource() instanceof JFormattedTextField){
                final JFormattedTextField jftf =
                        (JFormattedTextField) e.getSource();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        jftf.selectAll();
                    }});
            } else if (e.getSource() instanceof JTextComponent){
                JTextComponent jtf = (JTextComponent) e.getSource();
                jtf.selectAll();
            }

        } catch( Exception ex) { }
    }

    @Override
    public void focusLost(FocusEvent e){ }
}
